package com.yuyou.fn.activity.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yuyou.fn.activity.entity.Activity;
import com.yuyou.fn.activity.vo.ActivityTargetChangeVo;
import com.yuyou.fn.activity.vo.ActivityTargetDetailVo;
import com.yuyou.fn.activity.vo.ActivityTargetQueryVo;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.Campus;
import com.yuyou.fn.educational.service.ICampusService;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.yuyou.fn.activity.mapper.ActivityTargetMapper;
import com.yuyou.fn.activity.entity.ActivityTarget;
import com.yuyou.fn.activity.service.IActivityTargetService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * ActivityTarget 表数据服务层接口实现类
 *
 */
@Service
public class ActivityTargetServiceImpl extends SuperServiceImpl<ActivityTargetMapper, ActivityTarget,Long> implements IActivityTargetService {

    @Resource
    private ICampusService campusService;

    @Override
    public void updateActivityNameAndStartDateByActivityId(long activityId, String activityName, Date activityStartDate ) {
        ActivityTarget activityTarget=new ActivityTarget();
        activityTarget.setActivityName(activityName);
        activityTarget.setActivityStartDate(activityStartDate);
        Example<ActivityTarget> example=ActivityTarget.createExample();
        example.createCriteria().andEqualTo("activityId",activityId);
        this.updateSelectiveByExample(activityTarget,example);
    }

    @Override
    public List<ActivityTarget> findActivityTargetByActivityIdCampusIds(long activityId, List<Long> campusIds) {

        Example<ActivityTarget> example=ActivityTarget.createExample();
        example.createCriteria().andEqualTo("activityId",activityId).andIn("campusId",campusIds);
        return this.findListByExample(example);
    }

    @Override
    public ActivityTarget findActivityTargetByActivityIdCampusId(long activityId, long campusId) {
        Example<ActivityTarget> example=ActivityTarget.createExample();
        example.createCriteria().andEqualTo("activityId",activityId).andEqualTo("campusId",campusId);
        return this.findOneByExample(example);
    }

    @Override
    public void changeActivityTargetByVo(ActivityTargetChangeVo activityTargetChangeVo) {
        ActivityTarget activityTarget=new ActivityTarget();
        BeanUtils.copyProperties(activityTarget,activityTargetChangeVo);
        activityTarget.setUpdateTime(TimeUtils.now());
        this.updateSelectiveById(activityTarget);
    }

    @Override
    public Page<ActivityTargetDetailVo> findActivityTargetPageByQueryVo(DataPermission dataPermission, ActivityTargetQueryVo activityTargetQueryVo, int pageNo, int pageSize){
        Example<ActivityTarget> example=ActivityTarget.createExample();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,activityTargetQueryVo);
        criteria.andIn("campusId",dataPermission.getCampusIds());
        example.orderBy("createTime").desc();
        Page<ActivityTarget> activityTargetPage = this.findPageByExample(example, pageNo, pageSize);

        Page<ActivityTargetDetailVo> activityTargetDetailVoPage=Utils.newPage(activityTargetPage);
        List<ActivityTarget> activityTargetList=activityTargetPage.getRecords();
        if (activityTargetList!=null){
            List<Long> campusIds = BeanUtils.toList(activityTargetList, "campusId");
            List<Campus> campusList=campusService.findByIds(campusIds,"campusId","campusName");
            Map<Long,Campus> campusMap=BeanUtils.toMap(campusList,"campusId");
            List<ActivityTargetDetailVo> activityTargetDetailVoList= Lists.transform(activityTargetList,item->{
                ActivityTargetDetailVo activityTargetDetailVo=new ActivityTargetDetailVo();
                BeanUtils.copyProperties(activityTargetDetailVo,item);
                Campus campus=campusMap.get(item.getCampusId());
                activityTargetDetailVo.setCampusName(campus.getCampusName());
                return activityTargetDetailVo;
            });
            activityTargetDetailVoPage.setRecords(activityTargetDetailVoList);
        }
        return activityTargetDetailVoPage;
    }

    @Override
    public SXSSFWorkbook exportActivityTarget(DataPermission dataPermission, ActivityTargetQueryVo activityTargetQueryVo) {
        Page<ActivityTargetDetailVo> activityTargetPage=this.findActivityTargetPageByQueryVo(dataPermission,activityTargetQueryVo,1,10000);
        return activityTarget2excel(activityTargetPage.getRecords());
    }

    public static SXSSFWorkbook activityTarget2excel(List<ActivityTargetDetailVo> activityTargetDetailVoList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("活动目标设置");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{ "活动名称","校区","活动时间","活动总目标","新老生目标","目标转化率","是否开启签到"};

        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }

        int rowNum = 1;

        if (activityTargetDetailVoList!=null){
            for (ActivityTargetDetailVo activityTargetDetailVo : activityTargetDetailVoList) {
                int cellNum = 0;

                SXSSFRow row = sheet.createRow(rowNum++);

                SXSSFCell cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(activityTargetDetailVo.getActivityName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(activityTargetDetailVo.getCampusName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(activityTargetDetailVo.getActivityStartDate()==null?"": Utils.formatDate(activityTargetDetailVo.getActivityStartDate(),"yyyy-MM-dd"));


                cell = row.createCell(cellNum++);
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(activityTargetDetailVo.getTotalTargetNum()==null?0:activityTargetDetailVo.getTotalTargetNum());

                cell = row.createCell(cellNum++);
                cell.setCellType(CellType.NUMERIC);
                cell.setCellValue(activityTargetDetailVo.getNewOldTargetNum()==null?0:activityTargetDetailVo.getNewOldTargetNum());

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(activityTargetDetailVo.getTranferRate()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(activityTargetDetailVo.getSignStatus()==0?"未开启":"已开启");

            }
        }
        return excel;
    }

}